********************************************************************************
* Crisis at Home, Exit Abroad: Coups, CivilWars, and Coalition Defection       *
* Replication Material                                                         *
********************************************************************************

/*
This do-file replicates the empirical analysis for the manuscript:"Crisis at Home, Exit Abroad: Coups, Civil Wars, and Coalition Defection"

Submitted to: Foreign Policy Analysis
Author:       Weiss Mehrabi
Contact:      weiss.mehrabi@centre.edu

Replication Instructions:
1.  This script must be run in Stata.
2.  Place this .do file and the "fpa_dataset.dta" file in the
    SAME working directory.
3.  Run the script from start to finish. It will generate a log file
    ("replication_log.log") and all tables (.tex files) from the manuscript.
*/


********************************************************************************
* 0. SETUP
********************************************************************************

* Set Stata version for compatibility
version 17.0

* Clear memory and set preferences
clear all
set more off

* Open a log file to document the replication
log using "replication_log.log", replace text


********************************************************************************
* 1. LOAD REPLICATION DATA
********************************************************************************

* Load trimmed replication dataset
use "fpa_dataset.dta", clear


********************************************************************************
* 2. TABLE 1: SUMMARY STATISTICS
********************************************************************************

* Generate and store summary statistics
estpost summarize early_withdrawal fcoup scoup lbestdead coal1_state coal2_un ///
    coal3_alliance coalsize direct2 domdispute combat ///
    ltdistance lldistance logcap troops ///
    newll election dem dura time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7

* Export summary statistics to LaTeX
esttab using "summary_stats_pervar.tex", ///
    cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2))") ///
    label booktabs nonumber nomtitle unstack ///
    title("Summary Statistics") ///
    replace


********************************************************************************
* 3. TABLE 2: MAIN MODELS (MODELS 1–7)
********************************************************************************

* Clear any stored estimates
eststo clear

* Model 1: Liberal democracies not excluded
logit early_withdrawal fcoup scoup lbestdead ///
    if type_of_conflict != 1 & 2, cluster(intid) nolog
eststo m1

* Model 2: Liberal democracies excluded
logit early_withdrawal fcoup scoup lbestdead ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog
eststo m2

* Model 3: Liberal democracies excluded + controls
logit early_withdrawal fcoup scoup lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll dur time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog
eststo m3

* Model 4: Failed coup only (LD excluded + controls)
logit early_withdrawal fcoup coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll dur time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog
eststo m4

* Model 5: Successful coup only (LD excluded + controls)
logit early_withdrawal scoup coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll dur time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog
eststo m5

* Model 6: Battle deaths only (LD excluded + controls)
logit early_withdrawal lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll dur time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog
eststo m6

* Model 7: Liberal democracies not excluded + controls
logit early_withdrawal fcoup scoup lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll election dem dur time time2 time3 ///
    if type_of_conflict != 1 & 2, cluster(intid) nolog
eststo m7

* Export models to LaTeX
esttab m1 m2 m3 m4 m5 m6 m7 using "7_models.tex", ///
    replace se star(* 0.1 ** 0.05 *** 0.01) ///
    stats(N r2_p, labels("N" "Pseudo R²")) ///
    booktabs label compress ///
    title("Table 2: Early Withdrawal from Military Coalitions, 1950–2001")


********************************************************************************
* 4. FIGURES 1-2: MARGINAL EFFECTS CALCULATIONS
********************************************************************************

/*
Note: The "margins" commands below are run to calculate the substantive effects. The "marginsplot" commands are commented out, as the final figures for the manuscript were created in R for enhanced visualization using the results generated here. Please run the accompanying R script(s) separately to regenerate the two figures
*/

* Re-run Model 3 for margins
logit early_withdrawal fcoup scoup lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll dur time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog

* Marginal effects: Failed coup
margins, at(fcoup = (0 1))
* marginsplot

* Marginal effects: Successful coup
margins, at(scoup = (0 1))
* marginsplot

* Marginal effects: Battle deaths
margins, at(lbestdead = (0(1)13.11))
* marginsplot


********************************************************************************
* 5. APPENDIX: ROBUSTNESS CHECKS
********************************************************************************

eststo clear

* (A) Alternative DV windows – Non-consolidated sample
logit alt_dv_1 fcoup scoup lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll dur time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog
eststo R1_dv2

logit alt_dv_2 fcoup scoup lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll dur time time2 time3 ///
    if type_of_conflict != 1 & 2 & polity2 < 7, cluster(intid) nolog
eststo R2_dv3

* (B) Alternative DV windows – Full sample (+ election/dem)
logit alt_dv_1 fcoup scoup lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll election dem dur time time2 time3 ///
    if type_of_conflict != 1 & 2, cluster(intid) nolog
eststo R3_dv2_full

logit alt_dv_2 fcoup scoup lbestdead coal1_state coal3_alliance coalsize ///
    direct2 domdispute combat ltdistance lldistance logcap troops ///
    newll election dem dur time time2 time3 ///
    if type_of_conflict != 1 & 2, cluster(intid) nolog
eststo R4_dv3_full

* Export robustness results to LaTeX
esttab R1_dv2 R2_dv3 R3_dv2_full R4_dv3_full using "robustness_models.tex", ///
    replace se star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N r2_p, labels("N" "Pseudo R²")) ///
    booktabs label compress ///
    title("Appendix Table X: Robustness Checks (DV Windows, SEs, Full Sample)")


********************************************************************************
* 6. END OF DO-FILE
********************************************************************************

* Close the log file
log close

* End of script
exit
